package jsu.xxy.coffee3.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jdk.dynalink.linker.LinkerServices;
import jsu.xxy.coffee3.Utils.JwtUtil;
import jsu.xxy.coffee3.Utils.MD5;
import jsu.xxy.coffee3.domain.Customer;
import jsu.xxy.coffee3.domain.Login;
import jsu.xxy.coffee3.mapper.CustomerMapper;
import jsu.xxy.coffee3.service.impl.CustomerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RequestMapping("/customer")
@RestController
public class CustomerController {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerServiceImpl customerService;



    @GetMapping("/login")
    @Operation(summary = "顾客注册、登录")
    public Login login(@RequestParam String phone, @RequestParam String password){
        return customerService.login(phone,password);

    }

    @GetMapping("/checkToken")
    @Operation(summary = "检查登录")
    public Boolean checkToken(HttpServletRequest request){

        String token = request.getHeader("token");
        return JwtUtil.checkToken(token);
    }

    @GetMapping("getAll")
    @Operation(summary = "所有顾客信息")
    public Page<Customer> getAll(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
//            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        return customerService.getAll(pageNo,pageSize);

    }
    @GetMapping("selectByName")
    @Operation(summary = "顾客-姓名查询")
    public Page<Customer> selectByName(@RequestParam String name,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
//            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        return customerService.selectByName(name,pageNo,pageSize);

    }
    @GetMapping("selectByPhone")
    @Operation(summary = "顾客-手机号查询")
    public Page<Customer> selectByPhone(@RequestParam String phone,
                                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
//            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        return customerService.selectByPhone(phone,pageNo,pageSize);

    }
    @GetMapping("selectById")
    @Operation(summary = "顾客-编号查询")
    public Page<Customer> selectById(@RequestParam Integer id,
                                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
//            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        return customerService.selectById(id,pageNo,pageSize);

    }
    @PostMapping("/changeById")
    @Operation(summary = "顾客-修改")
    public boolean changeById(@RequestBody Customer customer){
        return customerService.changeById(customer);

    }
}
